package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DialogUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.OnItemClickInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.databinding.ActivityCollectViewBinding;

import app.fugema.shop.R;
import app.fugema.shop.main.center.model.CollectGoodsResult;
import app.fugema.shop.main.center.vm.UserVM;
import app.fugema.shop.main.invoice.adapter.CollectProductAdapter;
import app.fugema.shop.main.goods.view.GoodsDetailActivity;


public class CollectProductActivity extends BaseTitleActivity<ActivityCollectViewBinding, UserVM> implements OnGetDataListener
{
    private String userKey;

    private boolean isEdit = false;

    public static void start(Context context)
    {
        Intent intent = new Intent(context, CollectProductActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_view);
    }
    private CollectProductAdapter adapter;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("收藏的商品",R.color.white);
        setRightText("编辑",R.color.white);
        adapter = new CollectProductAdapter();
        binding.rvRecyclerView.setAdapter(adapter);
        userKey = LoginUtils.getToken();

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView,this,this);
        //编辑
        setRightTextOnClick(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                buttonTextChanged();
            }
        });

        adapter.setOnItemClickInterface(new OnItemClickInterface() {
            @Override
            public boolean onItemClick(View view, int onclickType, int itemType, int position, Object bean)
            {
              if(bean instanceof CollectGoodsResult.CollectGoods)
              {
                  switch ( view.getId() )
                  {
                      case R.id.btn_delete:
                          DialogUtils.showSimpleDialog(getActivity(), false, new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which)
                              {
                                  dialog.dismiss();
                                  cancelProductById(((CollectGoodsResult.CollectGoods)bean).getId());
                              }
                          }, "确认取消收藏？");
                          break;
                      default:
                          GoodsDetailActivity.start(view.getContext(),  ((CollectGoodsResult.CollectGoods)bean).getId());
                          break;
                  }
              }
                return false;
            }
        });

    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        onGetData(1);
    }

    @Override
    public void onGetData(int pn)
    {
        viewModel.getCollectionProduct(pn,userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<CollectGoodsResult>>(getActivity())
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<CollectGoodsResult> data)
                     {
                         viewUtils.setDataListRefreshLayout(adapter,pn,10,data.data.getData());
                     }

                     @Override
                     public boolean isEmptyData(LiveDataWrapper<CollectGoodsResult> data)
                     {
                         return pn==1 && UIUtils.isEmpty(data.data);
                     }
                 });
    }

    private void buttonTextChanged()
    {
        if (isEdit)
        {
            isEdit = false;
            setRightText("管理");
            adapter.setEdit( isEdit );
        }
        else
        {
            setRightText("保存");
            isEdit = true;
            adapter.setEdit( isEdit );
        }
    }

    private void cancelProductById(long productId)
    {
        viewModel.cancelCollectProduct(productId, userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                     {
                          if( data.data.isSuccess() )
                          {
                              UIUtils.showToastSafes( data.data.getMsg() );
                              onGetData(1);
                          }
                     }
                 });
    }

}
